# -*- coding: UTF-8 -*-

try:
    fh = open("testfile", "r")
    fh.write("这是一个测试文件，用于测试异常!!")
    print 11
except IOError:
    print "Error: 没有找到文件或读取文件失败"
else:
    print "内容写入文件成功"
    fh.close()

# 定义函数
def temp_convert(var,t = 'xxx'):
    try:
        return int(var) and int(t)
    except Exception, Argument:
        print "参数没有包含数字\n", Argument

# 调用函数
temp_convert("1");

# 定义函数
def mye( level ):
    if level < 1:
        raise Exception("Invalid level!", level, level, 1111)
        raise Exception,"Invalid level!"
        # 触发异常后，后面的代码就不会再执行
try:
    mye(0)            # 触发异常
except Exception,err:
    print 1,err
else:
    print 2

class Networkerror(RuntimeError):
    def __init__(self, arg):
        self.args = arg
try:
    raise Networkerror("Bad hostname")
except Networkerror as e:
    print e.args

